const validate = require('../../utils/validate')
const common = require('../../utils/common')
const { queryLeadShopOrderPage, getLeadApproveCount } = require('../../services/stores')
const dayjs = require('../../utils/dayjs')
const { getUserInfo } = require('../../utils/user')

const defaultPage = {
  curPage: 1,
  pageSize: 10,
}
Page({
  data: {
    shopVo: getUserInfo().shopVo,
    totalCount: 0,
    query: {
      ...defaultPage,
      serach: '',
    },
    dataList: [],
    loaded: true,
    total: 10,
  },
  onLoad: function () {
    this.updateQuery()
    this.getCount()
  },
  updateQuery: function (payload) {
    this.setData({
      query: { ...this.data.query, ...payload }
    }, this.getDataList)
  },
  getCount: function () {
    getLeadApproveCount().then((res) => {
      this.setData({
        totalCount: res.data.approveCount
      })
    }).catch((err) => {
      common.log(err)
    })
  },
  getDataList: function () {
    if (this.data.query.curPage > 1 && this.data.dataList.length >= this.data.total) return
    const params = { ...this.data.query }
    queryLeadShopOrderPage(params).then(res => {
      const { total } = res.page
      this.setData({ total })
      if (params.curPage === 1) {
        this.setData({
          dataList: res.data
        })
      } else {
        this.setData({
          dataList: [...this.data.dataList, ...res.data]
        })
      }
      this.setData({
        loaded: true
      })
    }).catch(err => {
      common.log(err)
    })
  },
  scrollBottom: function () {
    const curPage = ++this.data.query.curPage
    this.updateQuery({ curPage })
  },
  onSearch: function (e) {
    const value = e.detail.value
    this.updateQuery({ serach: value, ...defaultPage })
  },
  onCancel: function () {
    this.updateQuery({ serach: '', ...defaultPage })
  }
})